/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.krossk.servlets;

import br.com.krossk.dao.ArquivoDAO;
import br.com.krossk.jpa.JPAFactory;
import br.com.krossk.model.Arquivo;
import br.com.krossk.model.Repositorio;
import java.io.IOException;
import java.io.PrintWriter;
import javax.persistence.EntityManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.text.html.parser.Entity;

/**
 *
 * @author Henrique
 */
public class DeletarArquivo extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=windows-1252");
        PrintWriter out = response.getWriter();
        int idArquivo = Integer.parseInt(request.getParameter("idArquivo"));
        EntityManager em = JPAFactory.getEntityManager();

        try{

            
            
            Arquivo a = em.find(Arquivo.class, idArquivo);
            Repositorio r = em.find(Repositorio.class, a.getIdArquivo());
            em.getTransaction().begin();
            em.remove(r);
            em.remove(a);
            em.getTransaction().commit();
            out.println("<html>");
            out.println("<head><title>Arquivo Excluído</title></head>");
            out.println("<body bgcolor='#ccccff'>");
            out.println("<div style='background-color:#eee; color:#009; font-size: 16px; font-family: Arial; font-weight: 700; text-align: center'>Exclusão de Arquivo</div><br />");
            out.println("<font face='Arial' style='font-size:14px; color:#009'>O arquivo selecionado foi excluído!<br /><br />");
            out.print("<center><form action='/krossk/pages/arquivo/admin/consultaAdmin.jsp' target='_parent'><input type='button' value='OK' onclick='window.close();'></form></center>");
            out.println("</body>");
            out.println("</html>");

        }catch(Exception ex){
            out.println("<html>");
            out.println("<head><title>Arquivo Excluído</title></head>");
            out.println("<body bgcolor='#ccccff'>");
            out.println("<div style='background-color:#eee; color:#009; font-size: 16px; font-family: Arial; font-weight: 700; text-align: center'>Exclusão de Arquivo</div><br />");
            out.println("<font face='Arial' style='font-size:14px; color:#009'>Não foi possível excluir o arquivo selecionado!<br /><br />");
            out.print("<center><input type='button' value='Fechar' onclick='window.close();'></center>");
            out.println("</body>");
            out.println("</html>");
            em.getTransaction().rollback();
        } finally {
            
            em.close();
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
